package com.hhhn.wk.main.tab4;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.hhhn.wk.R;
import com.hhhn.wk.base.BaseActivity;
import com.hhhn.wk.entity.User;
import com.hhhn.wk.login.CheckActivity;
import com.hhhn.wk.utils.AllPublic;
import com.hhhn.wk.utils.Constants;
import com.hhhn.wk.utils.MD5Util;
import com.hhhn.wk.utils.SharePreferencesUser;
import com.hhhn.wk.utils.mi.AES;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class UpNameActivity extends BaseActivity {
    private LinearLayout ll_ok;//完成
    private EditText et_name;//输入
    private User user=null;
    private BaseActivity activity;
    private Context context;
    private Intent intent;
    @Override
    protected void setOnCreateContentView() {
        setContentView(R.layout.activity_up_name);
        context=UpNameActivity.this;
        activity=this;
         intent=getIntent();
    }
    @Override
    protected void initView() {
        ll_ok=findViewById(R.id.ll_ok);
        et_name=findViewById(R.id.et_name);
    }
    @Override
    protected void initData() {
        user = (User) SharePreferencesUser.getBean(activity, Constants.USER);
        if (user!=null){
            et_name.setText(user.getUserNickname());
        }else {
            et_name.setText("");
        }

    }
    @Override
    protected void initClick() {
        ll_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String trim = et_name.getText().toString().trim();
                int length = trim.length();
                if (length<2 ||length>12){
                    toastWk("用户昵称为2-12个字符");
                    return;
                }
                getchangeName();
            }
        });
    }
    //修改昵称
    public void getchangeName() {
        showLoad();
        RequestParams params = AllPublic.getParamsAccount(activity, "/changeAccount", false);

        try {
            JSONObject person=new JSONObject();
            person.put("id",user.getUid());
            person.put("account",et_name.getText().toString().trim());
            AES aes = AllPublic.jsonDesPackaging(person);
            params.addParameter("data",aes.getData());
            params.addParameter("key",aes.getKey());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //请求
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override  //成功
            public void onSuccess(String decode) {
                String result = AllPublic.jsonDesDecode(decode);//解密
                try {
                    JSONObject jsonObject= new JSONObject(result);
                    String reason = jsonObject.getString("code");
                    if (reason.equals("0")) {
                        user.setUserNickname(et_name.getText().toString().trim());

                        //存储
                        SharePreferencesUser.putBean(activity,Constants.USER, user);
                        toastWk("昵称修改成功");
                        setResult(1,intent);
                        finish();
                    }else {
                        toastWk(jsonObject.getString(getResources().getString(R.string.errMsg)));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dismissLoad();
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogWk("修改昵称-第二个："+ex + "");
                ex.printStackTrace();
                dismissLoad();
            }
            @Override
            public void onCancelled(CancelledException cex) {
                LogWk("修改昵称-第三个：");
                dismissLoad();
            }
            @Override
            public void onFinished() {
                LogWk("修改昵称-第四个：");
                dismissLoad();
            }
        });
    }
}
